package com.boarsoft.boar.batch.entity;

import com.boarsoft.common.Util;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 批量的执行
 *
 * @author Mac_J
 * @update 2023.12.25 部分字段修改以及新增子任务字段。by tao.wang57
 * @update 2024.01.13 新增取消及取消中的执行状态。by tao.wang57
 */
public class BatchExecution implements Serializable {
    public static final long serialVersionUID = 5121697045380748188L;

    /**
     * 执行类型：新跑
     */
    public static final short TYPE_START = 0;
    /**
     * 执行类型：续跑
     */
    public static final short TYPE_RESUME = 1;
    /**
     * 执行类型：重跑
     */
    public static final short TYPE_REDO = 2;

    /**
     * 初始状态，新创建，未分发，未入队
     */
    public static final short STATUS_NEW = 0;
    /**
     * 已在批量应用入队，等待执行
     */
    public static final short STATUS_QUEUED = 1;
    /**
     * 执行中
     */
    public static final short STATUS_RUNNING = 2;
    /**
     * 成功执行完毕
     */
    public static final short STATUS_SUCCESS = 3;
    /**
     * 执行失败
     */
    public static final short STATUS_FAILED = 4;
    /**
     * 已挂起
     */
    public static final short STATUS_SUSPEND = 5;
    /**
     * 正常中断中（暂停）
     */
    public static final short STATUS_STOPPING = 6;
    /**
     * 正常中断（暂停）
     */
    public static final short STATUS_STOPPED = 7;
    /**
     * 执行超时
     */
    public static final short STATUS_TIMEOUT = 8;
    /**
     * 取消中（取消）
     */
    public static final short STATUS_CANCELING = 10;
    /**
     * 取消（取消）
     */
    public static final short STATUS_CANCELED = 11;

    /**
     * 所需监息未找到
     */
    public static final int ERROR_NOT_FOUND = 1;
    /**
     * 状态错误
     */
    public static final int ERROR_WRONG_STATUS = 2;
    /**
     * 状态错误
     */
    public static final int ERROR_MISSING_INFO = 3;
    /**
     * 未知错误
     */
    public static final int ERROR_UNKNOWN = 4;


    /**
     * 批量执行记录主键（技术主键）
     */
    protected String id;
    /**
     * 批次号或任务id
     */
    protected String batchId;
    /**
     * 批量定义ID外键
     */
    protected String infoId;
    /**
     * 冗余自批量定义表
     */
    protected String code;
    /**
     * 冗余自批量定义表
     */
    protected String catalog;
    /**
     * 流程入口
     */
    protected String entry;
    /**
     * 开始执行时间
     */
    protected String startTime;
    /**
     * 结束执行时间
     */
    protected String endTime;
    /**
     * 执行状态
     */
    protected short status;
    /**
     * 第几次执行（重跑和续跑时）
     */
    protected short no = 1;
    /**
     * 错误监息
     */
    protected String error;
    /**
     * 执行机器IP
     */
    protected String addr;
    /**
     * 最近一次执行类型，0：新跑、1：续跑、2：重跑（即使之前是成功的）
     */
    protected short type;
    /**
     * 参数对象，Map<String, Object> 对应的JSON字符串
     */
    protected String params;
    /**
     * 流程执行步骤
     */
    protected String phrase;
    /**
     * 记录类别：0/null - 批次级别; 1 - 分片级别; 2 - 分页级别
     */
    protected short level;
    /**
     * 父级ID，当记录为批次记录时该字段为空
     */
    protected String parentId;
    /**
     * 任务层次路径<br>
     * 批次级别：默认为null<br>
     * 分页级别：/任务批次id/作业ID/分片批次id/分页批次id<br>
     */
    protected String path;
    /**
     * 执行进度，0-100
     */
    protected String progress;

    /**
     * 子任务数
     */
    private int children;
    /**
     * 子任务数据量
     */
    private int childDataVol;

    /**
     * 阶段占比、子任务占比
     */
    private short phrasePer;

    @Override
    public String toString() {
        return new StringBuilder().append(infoId == null ? "":infoId).append("/").append(no)//
                .append("/").append(type).append("/").append(id).toString();
    }

    public String getEntry() {
        return entry;
    }

    public void setEntry(String entry) {
        this.entry = entry;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public void setStatus(short status) {
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    public String getStartTime() {
        return this.startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public void setStartTime(long startTime) {
        Date d = new Date();
        d.setTime(startTime);
        this.setStartTime(d);
    }

    public void setStartTime(Date startTime) {
        this.startTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(startTime);
    }

    public String getEndTime() {
        return this.endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public void setEndTime(long endTime) {
        Date d = new Date();
        d.setTime(endTime);
        this.setEndTime(d);
    }

    public void setEndTime(Date endTime) {
        this.endTime = Util.date2str(endTime, Util.STDDTMF);
    }

    public void setEndTime() {
        this.endTime = Util.date2str(new Date(), Util.STDDTMF);
    }

    public short getStatus() {
        return this.status;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        if (error != null) {
            if (error.length() > 512) {
                error = error.substring(0, 512);
            }
            this.error = error;
        }
    }

    public short getNo() {
        return no;
    }

    public void setNo(short no) {
        this.no = no;
    }

    public short getType() {
        return type;
    }

    public void setType(short type) {
        this.type = type;
    }

    public String getParams() {
        return params;
    }

    public void setParams(String params) {
        this.params = params;
    }

    public String getCatalog() {
        return catalog;
    }

    public void setCatalog(String catalog) {
        this.catalog = catalog;
    }

    public String getInfoId() {
        return infoId;
    }

    public void setInfoId(String infoId) {
        this.infoId = infoId;
    }

    public short getLevel() {
        return level;
    }

    public void setLevel(short level) {
        this.level = level;
    }

    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public int getChildren() {
        return children;
    }

    public void setChildren(int children) {
        this.children = children;
    }

    public String getPhrase() {
        return phrase;
    }

    public void setPhrase(String phrase) {
        this.phrase = phrase;
    }

    public String getProgress() {
        return progress;
    }

    public void setProgress(String progress) {
        this.progress = progress;
    }

    public short getPhrasePer() {
        return phrasePer;
    }

    public void setPhrasePer(short phrasePer) {
        this.phrasePer = phrasePer;
    }

    public String getBatchId() {
        return batchId;
    }

    public void setBatchId(String batchId) {
        this.batchId = batchId;
    }

    public int getChildDataVol() {
        return childDataVol;
    }

    public void setChildDataVol(int childDataVol) {
        this.childDataVol = childDataVol;
    }
}
